package vip.xiaonuo.common.mvc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.annotation.CommonNoRepeat;
import vip.xiaonuo.common.mvc.service.BaseService;
import vip.xiaonuo.common.page.PageQuery;
import vip.xiaonuo.common.pojo.CommonResult;

/**
 * 通用的 RESTful 控制器，提供基本的增删改查操作。
 *
 * @param <Bo>      业务对象类型
 * @param <Vo>      视图对象类型
 * @param <Q>       分页查询参数类型
 * @param <Service> 服务类型
 * @author zjt
 * @date 2025/3/18 09:29
 */
@RequiredArgsConstructor
public class BaseController<Bo, Vo, Q extends PageQuery, Service extends BaseService<Bo, Vo, Q>> {

    protected final Service baseService;

    /**
     * 分页查询
     */
    @PostMapping("/pageList")
    public CommonResult<Page<Vo>> pageList(@RequestBody Q pageQuery) {
        Page<Vo> page = baseService.pageList(pageQuery);
        return CommonResult.data(page);
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/detail/{id}")
    public CommonResult<Vo> detail(@PathVariable Long id) {
        Vo vo = baseService.selectVoById(id);
        return CommonResult.data(vo);
    }

    /**
     * 新增实体
     */
    @PostMapping("/add")
    @CommonLog("新增实体")
    @CommonNoRepeat
    public CommonResult<Void> add(@RequestBody Bo bo) {
        baseService.insert(bo);
        return CommonResult.ok();
    }

    /**
     * 修改实体
     */
    @PostMapping("/edit")
    @CommonLog("修改实体")
    @CommonNoRepeat
    public CommonResult<Void> update(@RequestBody Bo bo) {
        baseService.update(bo);
        return CommonResult.ok();
    }

    /**
     * 根据ID删除实体
     */
    @GetMapping("/del/{id}")
    @CommonLog("根据ID删除实体")
    public CommonResult<Void> delete(@PathVariable Long id) {
        baseService.delete(id);
        return CommonResult.ok();
    }
}